﻿using CommandProcessor.Dispatcher;
using Data.Repositories;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using PagedList;
using AutoMapper;
using QuanLySanXuat.Models;
using Domain.Commands;
using Core.Common;
using Core.Extensions;
using Data.Models;
namespace QuanLySanXuat.Controllers
{
    public class ToolNormController : Controller
    {
        private readonly ICommandBus commandBus;
        private readonly IToolNormRepository ToolNormRepository;
        private readonly IUOMRepository uomRepository;
        private readonly IToolTypeRepository toolTypeRepository;
        public ToolNormController(ICommandBus commandBus, IToolNormRepository ToolNormRepository, IUOMRepository uomRepository, IToolTypeRepository toolTypeRepository)
        {
            ModelState.Clear();
            this.commandBus = commandBus;
            this.ToolNormRepository = ToolNormRepository;
            this.uomRepository = uomRepository;
            this.toolTypeRepository = toolTypeRepository;
        }
        //
        // GET: /ToolNorm/
        [HttpGet]
        public ActionResult Index()
        {
            var prodcutType = ToolNormRepository.GetAll();
            ViewData["Level"] = toolTypeRepository.GetAll();
            ViewData["UOM"] = uomRepository.GetAll();
            return View(prodcutType.ToPagedList(1, 10));
        }
        [HttpGet]
        public ActionResult Create()
        {
            ModelState.Clear();
            return View();
        }
        [HttpPost]
        public ActionResult Save(ToolNormFormModels frm)
        {
            var form = new ToolNormFormModels()
            {
                ID = frm.ID,
                Date1=frm.Date1,
                Date2=frm.Date2,
                ToolTypeID=frm.Level,
                Norm=frm.Norm,
                UOMID = frm.UOMID
            };
            var command = Mapper.Map<ToolNormFormModels, CreateOrUpdateToolNormCommand>(form);
            IEnumerable<ValidationResult> errors = commandBus.Validate(command);
            ModelState.AddModelErrors(errors);
             var result = commandBus.Submit(command);
            
            var model = ToolNormRepository.GetAll();
            return PartialView("_ToolNormList", model.ToPagedList(1, 10));
        }

        [HttpGet]
        public JsonResult Get(int ID)
        {
            ToolNorm product = ToolNormRepository.GetById(ID);
            var obj = Mapper.Map<ToolNorm, ToolNormFormModels>(product);
            return Json(obj, JsonRequestBehavior.AllowGet);
        }

        [HttpPost]
        public ActionResult RemoveItems(string[] ProID)
        {
            List<DeleteToolNormCommand> list = new List<DeleteToolNormCommand>();
            foreach (var item in ProID)
            {
                var command = new DeleteToolNormCommand { ID = Convert.ToInt16(item) };
                list.Add(command);
            }
            var result = commandBus.SubmitRange(list);
            var model = ToolNormRepository.GetAll();
            return PartialView("_ToolNormList", model.ToPagedList(1, 10));
        }
        [HttpGet]
        public ActionResult Search(string Name)
        {
            IEnumerable<ToolNorm> model;
            if (String.IsNullOrEmpty(Name))
            {
                model = ToolNormRepository.GetAll();
            }
            else
            {
                model = ToolNormRepository.GetMany(x => (x.ToolType.Name.Contains(Name)));
            }
            return PartialView("_ToolNormList", model.ToPagedList(1, 10));
        }
        [HttpGet]
        [ValidateInput(false)]
        public ActionResult Page(int PageID)
        {
            var prodcutType = ToolNormRepository.GetAll();
            return PartialView("_ToolNormList", prodcutType.ToPagedList(1, PageID));
        }
    }
}
